整合到Springboot項(xiàng)目流程
1、添加pom依賴
<dependency>
? ? <groupId>com.github.tobato</groupId>
? ? <artifactId>fastdfs-client</artifactId>
? ? <version>1.25.2-RELEASE</version>
</dependency>
2、將Fdfs配置引入項(xiàng)目
@Import(FdfsClientConfig.class)
@SpringBootApplication
public class JingtongApplication {
? ? public static void main(String[] args) {
? ? ? ? SpringApplication.run(JingtongApplication.class, args);
? ? }
}
3、在spring配置文件中加入fdfs相關(guān)配置
根據(jù)項(xiàng)目當(dāng)中使用配置文件類型(.yml和.properties選擇其中一個(gè)),加入相應(yīng)的配置。
application.yml
fdfs:
? soTimeout: 1500
? connectTimeout: 600
? thumbImage: ? ? ? ? ? ? #縮略圖生成參數(shù)
? ? width: 150
? ? height: 150
? trackerList: ? ? ? ? ? ?#TrackerList參數(shù),支持多個(gè)
? ? - 192.168.0.201:22122
? ? - 192.168.0.202:22122?
4、在項(xiàng)目中使用
客戶端主要包括以下接口:
TrackerClient - TrackerServer接口
GenerateStorageClient - 一般文件存儲(chǔ)接口 (StorageServer接口)
FastFileStorageClient - 為方便項(xiàng)目開發(fā)集成的簡單接口(StorageServer接口)
AppendFileStorageClient - 支持文件續(xù)傳操作的接口 (StorageServer接口)
package com.digi_zones.support.fs;
import com.digi_zones.config.AppConfig;
import com.digi_zones.contacts.AppConstants;
import com.github.tobato.fastdfs.domain.FileInfo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
/**
?* <p>Description: FastDFS文件上傳下載包裝類</p>
?* <p>Copyright: Copyright (c) 2016</p>
?*
?* @author 楊信
?* @version 1.0
?* @date 2016/9/7
?*/
@Component
public class FastDFSClientWrapper {
? ? private final Logger logger = LoggerFactory.getLogger(FastDFSClientWrapper.class);
? ? @Autowired
? ? private FastFileStorageClient storageClient;
? ? @Autowired
? ? private AppConfig appConfig; ? // 項(xiàng)目參數(shù)配置
? ? /**
? ? ?* 上傳文件
? ? ?* @param file 文件對(duì)象
? ? ?* @return 文件訪問地址
? ? ?* @throws IOException
? ? ?*/
? ? public String uploadFile(MultipartFile file) throws IOException {
? ? ? ? StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
? ? ? ? return getResAccessUrl(storePath);
? ? }
? ? /**
? ? ?* 將一段字符串生成一個(gè)文件上傳
? ? ?* @param content 文件內(nèi)容
? ? ?* @param fileExtension
? ? ?* @return
? ? ?*/
? ? public String uploadFile(String content, String fileExtension) {
? ? ? ? byte[] buff = content.getBytes(Charset.forName("UTF-8"));
? ? ? ? ByteArrayInputStream stream = new ByteArrayInputStream(buff);
? ? ? ? StorePath storePath = storageClient.uploadFile(stream,buff.length, fileExtension,null);
? ? ? ? return getResAccessUrl(storePath);
? ? }
? ? // 封裝圖片完整URL地址
? ? private String getResAccessUrl(StorePath storePath) {
? ? ? ? String fileUrl = AppConstants.HTTP_PRODOCOL + appConfig.getResHost()
? ? ? ? ? ? ? ? + ":" + appConfig.getFdfsStoragePort() + "/" + storePath.getFullPath();
? ? ? ? return fileUrl;
? ? }
? ? /**
? ? ?* 刪除文件
? ? ?* @param fileUrl 文件訪問地址
? ? ?* @return
? ? ?*/
? ? public void deleteFile(String fileUrl) {
? ? ? ? if (StringUtils.isEmpty(fileUrl)) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? StorePath storePath = StorePath.praseFromUrl(fileUrl);
? ? ? ? ? ? storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
? ? ? ? } catch (FdfsUnsupportStorePathException e) {
? ? ? ? ? ? logger.warn(e.getMessage());
? ? ? ? }
? ? }
}
除了FastDFSClientWrapper類中用到的api,客戶端提供的api還有很多,可根據(jù)自身的業(yè)務(wù)需求,將其它接口也添加到工具類中即可。如下所示:
// 上傳文件,并添加文件元數(shù)據(jù)
StorePath uploadFile(InputStream inputStream, long fileSize, String fileExtName, Set<MateData> metaDataSet);
// 獲取文件元數(shù)據(jù)
Set<MateData> getMetadata(String groupName, String path);
// 上傳圖片并同時(shí)生成一個(gè)縮略圖
StorePath uploadImageAndCrtThumbImage(InputStream inputStream, long fileSize, String fileExtName,
? ? ? ? ? ? Set<MateData> metaDataSet);
// 。。。
在項(xiàng)目中使用FastDFSClientWrapper:
@Controller
public class MyController {
? ? @Autowired
? ? private FastDFSClientWrapper dfsClient;
? ? // 上傳圖片
? ? @RequestMapping(value = "/upload", method = RequestMethod.POST)
? ? public String upload(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception {
? ? ? ? // 省略業(yè)務(wù)邏輯代碼。。。
? ? ? ? String imgUrl = dfsClient.uploadFile(file);
? ? ? ? // 。。。。
? ? ? ? return xxxx;
? ? }
}















